//
//  NativeStoreHeadCell.m
//  ebook
//
//  Created by mt on 17/1/10.
//  Copyright © 2017年 Moton. All rights reserved.
//

#define kFont 15.0  //字号
#define kFunctionBtnTag 100 //立即阅读 加入书架  全本缓存
#define kOptionBtnTag   200 //收藏 目录 推荐 分享

#import "NativeStoreHeadCell.h"
#import "Book.h"
#import "UIImageView+WebCache.h"

@interface NativeStoreHeadCell ()

@property (nonatomic, strong) UIImageView *bookCoverImv;//封面
@property (nonatomic, strong) UILabel *bookNameLabel;   //书名
@property (nonatomic, strong) UILabel *bookAuthorLabel; //作者
@property (nonatomic, strong) UILabel *bookTypeLabel;   //类型
@property (nonatomic, strong) UILabel *bookStatusLabel; //状态
@property (nonatomic, strong) UILabel *bookWordsLabel;  //字数
@property (nonatomic, strong) UILabel *bookRecentLabel; //最新

@end

@implementation NativeStoreHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self drawView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)drawView {
    
    [self addBookCoverImv];
    [self addSubview:self.bookNameLabel];
    [self addSubview:self.bookAuthorLabel];
    [self addSubview:self.bookTypeLabel];
    [self addSubview:self.bookStatusLabel];
    [self addSubview:self.bookWordsLabel];
    [self addSubview:self.bookRecentLabel];
    [self drawFunctionBtns];
    [self drawOptionBtns];
}

- (void)addBookCoverImv {
    UIImageView *backImv = [[UIImageView alloc] init];
    backImv.frame = CGRectMake(15, 15, 100, 130);
    backImv.image = [UIImage imageNamed:@"bookcellBackground"];
    [self addSubview:backImv];
    
    UIImageView *coverImv = [[UIImageView alloc] init];
    coverImv.frame = CGRectMake(2, 2, CGRectGetWidth(backImv.frame)-4, CGRectGetHeight(backImv.frame)-4);
    [backImv addSubview:coverImv];
    _bookCoverImv = coverImv;
}

- (UILabel *)bookNameLabel {
    if (!_bookNameLabel) {
        _bookNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15+CGRectGetWidth(_bookCoverImv.frame)+12, 15, (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 25)];
        _bookNameLabel.font = [UIFont systemFontOfSize:kFont];
        _bookNameLabel.textColor = [UIColor blackColor];
    }
    return _bookNameLabel;
}

- (UILabel *)bookAuthorLabel {
    if (!_bookAuthorLabel) {
        _bookAuthorLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookNameLabel.frame), (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 20)];
        _bookAuthorLabel.font = [UIFont systemFontOfSize:kFont-2];
        _bookAuthorLabel.textColor = [UIColor darkGrayColor];
    }
    return _bookAuthorLabel;
}

- (UILabel *)bookTypeLabel {
    if (!_bookTypeLabel) {
        _bookTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookAuthorLabel.frame), (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 20)];
        _bookTypeLabel.font = [UIFont systemFontOfSize:kFont-2];
        _bookTypeLabel.textColor = [UIColor darkGrayColor];
    }
    return _bookTypeLabel;
}

- (UILabel *)bookStatusLabel {
    if (!_bookStatusLabel) {
        _bookStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookTypeLabel.frame), (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 20)];
        _bookStatusLabel.font = [UIFont systemFontOfSize:kFont-2];
        _bookStatusLabel.textColor = [UIColor darkGrayColor];
    }
    return _bookStatusLabel;
}

- (UILabel *)bookWordsLabel {
    if (!_bookWordsLabel) {
        _bookWordsLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookStatusLabel.frame), (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 20)];
        _bookWordsLabel.font = [UIFont systemFontOfSize:kFont-2];
        _bookWordsLabel.textColor = [UIColor darkGrayColor];
    }
    return _bookWordsLabel;
}

- (UILabel *)bookRecentLabel {
    if (!_bookRecentLabel) {
        _bookRecentLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bookNameLabel.frame), CGRectGetMaxY(self.bookWordsLabel.frame), (MTScreenW-CGRectGetWidth(_bookCoverImv.frame)-30-15), 20)];
        _bookRecentLabel.font = [UIFont systemFontOfSize:kFont-2];
        _bookRecentLabel.textColor = [UIColor darkGrayColor];
    }
    return _bookRecentLabel;
}

- (void)setModel:(NativeOrderlistModel *)model {

    [self.bookCoverImv sd_setImageWithURL:[NSURL URLWithString:model.coverpicurl]];
    self.bookNameLabel.text = model.bookname;
    self.bookAuthorLabel.text = [NSString stringWithFormat:@"作者：%@",model.author];
    self.bookTypeLabel.text = [NSString stringWithFormat:@"分类：%@", model.sortname];
    self.bookStatusLabel.text = [NSString stringWithFormat:@"状态：%@", [model.fullflag intValue]==0?@"连载中":@"已完结"];
    self.bookWordsLabel.text = [NSString stringWithFormat:@"字数：%dK", [model.size intValue]/1000];
    self.bookRecentLabel.text = [NSString stringWithFormat:@"最新：%@", model.lastchapter];
}

-(void)setIsInBookcaseStatus:(BOOL)isInCase {
    
    UIButton *btn = [self viewWithTag:kFunctionBtnTag+1];
    btn.selected = isInCase;
    btn.backgroundColor = isInCase?MTConfigColor(@"MTNavigationBar_background_Color"):MTColor(247, 247, 247, 1);
    MTLog(@"%@", btn.titleLabel.text);
}

- (void)drawFunctionBtns {
    NSMutableArray *array = [NSMutableArray array];
    NSArray *btnTitles = @[@"立即阅读", @"加入书架", @"全本缓存"];
    CGFloat btnWidth = (MTScreenW-17*4)/3;
    for (int i = 0; i < btnTitles.count; i++) {
        UIButton *btn = [self fBtnWithFrame:CGRectMake(17+(17+btnWidth)*i, 15+CGRectGetMaxY(self.bookCoverImv.frame)+2+20, btnWidth, 40) title:btnTitles[i] tag:i+kFunctionBtnTag];
        [self addSubview:btn];
        [array addObject:btn];
    }
}

- (UIButton *)fBtnWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateSelected];
    [btn setTitleColor:MTColor(177, 177, 177, 1) forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    btn.backgroundColor = MTColor(247, 247, 247, 1);
    btn.titleLabel.font = [UIFont systemFontOfSize:kFont-2];
    btn.tag = tag;
    if (btn.tag==0) {
        btn.selected = YES;
        btn.backgroundColor = MTConfigColor(@"MTNavigationBar_background_Color");
    }
    [btn addTarget:self action:@selector(fBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)fBtnClick:(UIButton *)btn {
    
    btn.selected = YES;
    btn.backgroundColor = MTConfigColor(@"MTNavigationBar_background_Color");
    if ([self.delegate respondsToSelector:@selector(nativeStoreHeadCell:btnClickedWithTag:)]) {
        [self.delegate nativeStoreHeadCell:self btnClickedWithTag:btn.tag];
    }
}

- (void)drawOptionBtns {
    
    NSArray *titles = @[@"收藏", @"目录", @"推荐", @"分享"];
    NSArray *images = @[@"bookcollec", @"bookmenu", @"bookrec", @"bookshare"];
    CGFloat btnWidth = (MTScreenW-17*2)/4;
    for (int i = 0; i < titles.count; i++) {
        UIButton *btn = [self oBtnWithFrame:CGRectMake(17+btnWidth*i, 15+CGRectGetMaxY(self.bookCoverImv.frame)+2+20+40+20-5, btnWidth, 60) title:titles[i] image:images[i] tag:i+kOptionBtnTag];
        [self addSubview:btn];
    }
}

- (UIButton *)oBtnWithFrame:(CGRect)frame title:(NSString *)title image:(NSString *)image tag:(NSInteger)tag {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:kFont-2];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 2, btn.imageView.height)];
    line.backgroundColor = MTColor(247, 247, 247, 1);
    if (tag>0) [btn addSubview:line];
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(-(btn.height-btn.imageView.height)/2, (btn.width-btn.titleLabel.width)/2, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(btn.height-btn.titleLabel.height, -(btn.width-btn.titleLabel.width)/3, 0, 0);
    btn.tag = tag;
    [btn addTarget:self action:@selector(oBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)oBtnClick:(UIButton *)btn {

    if ([self.delegate respondsToSelector:@selector(nativeStoreHeadCell:btnClickedWithTag:)]) {
        [self.delegate nativeStoreHeadCell:self btnClickedWithTag:btn.tag];
    }
}



@end
